package com.yanfan.accessControl.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import javax.validation.Valid;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yanfan.common.utils.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yanfan.common.annotation.Log;
import com.yanfan.common.core.controller.BaseController;
import com.yanfan.common.core.domain.AjaxResult;
import com.yanfan.common.enums.BusinessType;
import com.yanfan.accessControl.domain.IbmsAccessFlow;
import com.yanfan.accessControl.service.IIbmsAccessFlowService;
import com.yanfan.common.utils.poi.ExcelUtil;
import com.yanfan.common.core.page.TableDataInfo;

/**
 * 门禁流量每小时统计Controller
 *
 * @author genluo
 * @date 2024-09-27
 */
@Api(tags = "门禁流量每小时统计")
@RestController
@RequestMapping("/access/flow")
public class IbmsAccessFlowController extends BaseController {
    @Autowired
    private IIbmsAccessFlowService ibmsAccessFlowService;

    /**
     * 查询门禁流量每小时统计列表
     */
    @ApiOperation("门禁流量每小时统计分页列表")
    @PreAuthorize("@ss.hasPermi('access:flow:list')")
    @GetMapping("/list")
    public TableDataInfo list(IbmsAccessFlow ibmsAccessFlow) {
        startPage();
        List<IbmsAccessFlow> list = ibmsAccessFlowService.queryPageList(ibmsAccessFlow);
        return getDataTable(list);
    }

    /**
     * 导出门禁流量每小时统计列表
     */
    @ApiOperation("导出门禁流量每小时统计列表")
    @PreAuthorize("@ss.hasPermi('access:flow:export')")
    @Log(title = "门禁流量每小时统计", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, IbmsAccessFlow ibmsAccessFlow) {
        List<IbmsAccessFlow> list = ibmsAccessFlowService.queryPageList(ibmsAccessFlow);
        ExcelUtil<IbmsAccessFlow> util = new ExcelUtil<IbmsAccessFlow>(IbmsAccessFlow.class);
        util.exportExcel(response, list, "门禁流量每小时统计数据");
    }

    /**
     * 获取门禁流量每小时统计详细信息
     */
    @ApiOperation("获取门禁流量每小时统计详细信息")
    @PreAuthorize("@ss.hasPermi('access:flow:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(ibmsAccessFlowService.getById(id));
    }

    /**
     * 新增门禁流量每小时统计
     */
    @ApiOperation("新增门禁流量每小时统计")
    @PreAuthorize("@ss.hasPermi('access:flow:add')")
    @Log(title = "门禁流量每小时统计", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody IbmsAccessFlow ibmsAccessFlow) {
        return toAjax(ibmsAccessFlowService.save(ibmsAccessFlow));
    }

    /**
     * 修改门禁流量每小时统计
     */
    @ApiOperation("修改门禁流量每小时统计")
    @PreAuthorize("@ss.hasPermi('access:flow:edit')")
    @Log(title = "门禁流量每小时统计", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody IbmsAccessFlow ibmsAccessFlow) {
        return toAjax(ibmsAccessFlowService.updateById(ibmsAccessFlow));
    }

    /**
     * 删除门禁流量每小时统计
     */
    @ApiOperation("删除门禁流量每小时统计")
    @PreAuthorize("@ss.hasPermi('access:flow:remove')")
    @Log(title = "门禁流量每小时统计", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(ibmsAccessFlowService.removeBatchByIds(Arrays.asList(ids)));
    }

    @GetMapping("/totalflow")
    @ApiOperation("禁流量统计")
    public AjaxResult accessFlowHour() {
        String now = DateUtils.parseDateToStr("yyyy-MM-dd", new Date());
//        List<IbmsAccessFlow> list = ibmsAccessFlowService.list(new LambdaQueryWrapper<IbmsAccessFlow>()
//                .like(IbmsAccessFlow :: getCountTime, now));
        return toAjax(ibmsAccessFlowService.getCountFlow(now));
    }
}
